package com.tuoren.forward.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
@MapperScan(basePackages = "com.tuoren.forward.mapper", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfiguration {

    /**
     * 指定mapper xml文件路径
     */
    public static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

    /**
     * @Author: lequal
     * @Description 获取一级的属性
     * @Date 2022/12/22 16:48
     * @return org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
     */
    @Primary
    @Bean("primaryDataSourceProperties")
    @ConfigurationProperties("spring.datasource.primary")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * @Author: lequal
     * @Description 获取下一级的属性(hikari)并创建数据源
     * @Date 2022/12/22 16:48
     * @return com.zaxxer.hikari.HikariDataSource
     */
    @Primary
    @Bean("primaryDataSource")
    @Qualifier(value = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary.hikari")
    public HikariDataSource primaryDataSource(@Qualifier("primaryDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

	/**
     * @Author: lequal
     * @Description 配置事务管理器
     * @Date 2022/12/22 16:48
     * @param dataSource
     * @return org.springframework.transaction.PlatformTransactionManager
     */
    @Bean(name = "primaryTransactionManager")
    @Primary
    public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * @Author: lequal
     * @Description 自定义SQLSession工厂
     * @Date 2022/12/22 16:50
     * @param dataSource
     * @return org.apache.ibatis.session.SqlSessionFactory
     */
    @Primary
    @Bean(name = "primarySqlSessionFactory")
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
    	final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(PrimaryDataSourceConfiguration.MAPPER_LOCATION));
        sessionFactoryBean.setTypeAliasesPackage("com.tuoren.forward.entity,com.tuoren.forward.entity.dto,com.tuoren.forward.entity.req,com.tuoren.forward.entity.resp");
        return sessionFactoryBean.getObject();
    }

    /**
     * @Author: lequal
     * @Description 创建JDBC模板
     * @Date 2022/12/22 16:48
     * @param dataSource
     * @return org.springframework.jdbc.core.JdbcTemplate
     */
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}